// pages/create-diary/create-diary.js
import { Config } from '../../utils/config.js';
import { Diary } from '../diary/diary-model.js';
var diary = new Diary();

Page({

  chooseImage: function () {
    var self = this

    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      success: function (res) {

        var imageSrc = res.tempFilePaths[0]

        wx.uploadFile({
          url: Config.restUrl + 'file/upload',
          filePath: imageSrc,
          name: 'file',
          success: function (res) {
            var res = JSON.parse(res.data);
            if (res.code) {
              wx.showToast({
                title: '上传成功',
                icon: 'success',
                duration: 1000
              })

              self.setData({
                imageSrc: res.file
              })
            } else {
              wx.showToast({
                title: res.error,
                icon: 'none',
                duration: 1000
              })
            }
          },
          fail: function ({ errMsg }) {
            wx.showToast({
              title: errMsg,
              icon: 'none',
              duration: 2000
            })
          }
        })

      },
      fail: function ({ errMsg }) {
        console.log('chooseImage fail, err is', errMsg)
      }
    })
  },

  /**
   * 页面的初始数据
   */
  data: {
    multiArray: [['0岁', '1岁', '2岁', '3岁', '4岁', '5岁', '6岁', '7岁', '8岁', '9岁'], ['0个月', '1个月', '2个月', '3个月', '4个月', '5个月', '6个月', '7个月', '8个月', '9个月', '10个月', '11个月', '12个月']],
    multiIndex: [0, 0],
  },

  bindMultiPickerChange: function (e) {
    this.setData({
      multiIndex: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    this.data.id = id;
    this.setData({
      pets_id: this.data.id
    })
  },

  formSubmit: function (e) {
    if (!e.detail.value.age) {
      wx.showToast({
        title: '请填写宠物年龄',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (!e.detail.value.content) {
      wx.showToast({
        title: '请输入内容',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (!e.detail.value.img) {
      wx.showToast({
        title: '请上传宠物照片',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (e.detail.value.age.length > 10) {
      wx.showToast({
        title: '年龄不超过10个字',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (e.detail.value.content.length > 100) {
      wx.showToast({
        title: '内容不超过100个字',
        icon: 'none',
        duration: 2000
      })
      return false;
    }

    diary.createDiary(e.detail.value, (data) => {
      if (data.code) {
        wx.showToast({
          title: data.info,
          icon: 'success',
          duration: 2000
        })
        setTimeout(function () {
          wx.navigateBack();
        }, 2000)

      } else {
        wx.showToast({
          title: data.error,
          icon: 'none',
          duration: 2000
        })
      }
    });
  }, 

})